package test.generator;

import java.util.ArrayList;
import java.util.List;

import test.configuration.ConfigFactory;
import test.domain.Roster;
import test.domain.User;
import test.domain.environment.MetaUser;
import test.generator.writer.DBWriter;
import test.generator.writer.Writer;

public class UserGenerator {
	private int maxCompanyCount;
	private int maxUserCount;
	
	private int maxDigit;
	private int rosterId;
	
	private String password;
	private String creationDate;
	
	private Writer writer;
	
	public UserGenerator() {
		MetaUser meta = ConfigFactory.getConfiguration().getMetaUser();
		this.initialize(meta);
	}
	
	public UserGenerator initialize(MetaUser meta) {
		this.maxCompanyCount = meta.getMaxCompanyCount();
		this.maxUserCount = meta.getMaxUserCount();
		this.rosterId = meta.getRosterStartId();
		this.maxDigit = meta.getUserMaxDigit();
		this.password = meta.getUserPassword();
		this.creationDate = meta.getUserCreationDate();
		
		return this;
	}
	
	public List<User> generateUsers() {
		List<User> users = new ArrayList<User>();
		
		String username;
		User user;
		List<Roster> rosters;
		
		int startNumber = 0;
		int endNumber = 0;
		
		int totalUserCount = maxCompanyCount * maxUserCount;
		
		for(int i = 1; i <= maxCompanyCount; i++) {
			endNumber = i * maxUserCount;
			startNumber = endNumber - maxUserCount;
			
			for (int j = startNumber; j < endNumber; j++) {
				// Create user row
				if (j > totalUserCount) break;
				
				List<String> jids = new ArrayList<String>();
				
				for (int k = startNumber; k < endNumber; k++) {
					if (k > totalUserCount) break;
					
					if (j == k) continue;
					jids.add(this.getUsername(String.valueOf(k)) + "@openfire");
				}
				
				username = this.getUsername(String.valueOf(j));
				rosters = this.generateRosters(username, jids);
				user = this.generateUser(username, rosters);
				
				users.add(user);
			}
		}
		
		writer.write(users);
		System.out.println("Writing is done.");
		
		return users;
	}
	
	private User generateUser(String username, List<Roster> rosters) {
		User user = new User();
		
		user.setUsername(username);
		user.setPlainPassword(password);
		user.setEmail(username + "@lguplus.co.kr");
		user.setName(username);
		user.setCreationDate(creationDate);
		user.setModifiedDate("0");
		user.setEncryptedPassword(null);
		user.setRosters(rosters);
		
		return user;		
	}
	
	private List<Roster> generateRosters(String username, List<String> jids) {
		List<Roster> rosters = new ArrayList<Roster>();
		
		for (String jid : jids) {
			Roster roster = new Roster();
			
			roster.setId(rosterId++);
			roster.setUsername(username);
			roster.setJid(jid);
			roster.setNick(jid.split("@")[0]);
			
			rosters.add(roster);
		}
		
		return rosters;
	}	
	
	public String getUsername(String index) {
		String number = index;
		
		if (index.length() < maxDigit) {
			for (int i = 0; i < maxDigit - index.length(); i++) {
				number = "0" + number;
			}
		}
		
		return "user" + number;
	}
	
	public String getPassword() {
		return password;
	}

	public String getCreationDate() {
		return creationDate;
	}
	
	public void setWriter(Writer writer) {
		this.writer = writer;
	}

	public static void main(String[] args) {
		UserGenerator generator = new UserGenerator();
		generator.setWriter(new DBWriter());
		
		generator.generateUsers();		
	}
}
